<?php
/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2021/8/3 上午11:05
 *@说明:首页控制器
 */

namespace app\store\controller;


use app\common\util\AdminException;
use think\captcha\facade\Captcha;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class Index extends Base
{
    public function index()
    {
        $today = strtotime(date("Y-m-d"));
        $count['send_order'] = Db::name('order')->where("order_status=1 and store_id=$this->dianpu_id")->count();//待发货订单
        $count['new_order'] = Db::name('order')->where('store_id',$this->dianpu_id)->whereDay("add_time")->count();//今日新增订单
        $count['order'] = Db::name('order')->where('store_id',$this->dianpu_id)->count();//订单总数
        $count['foster_send_order'] = Db::name('foster_order')->where("order_status=1 and store_id=$this->dianpu_id")->count();//认养中订单
        $count['foster_new_order'] = Db::name('foster_order')->where('store_id',$this->dianpu_id)->whereDay("add_time")->count();//今日新增认养订单
        $count['foster_order'] = Db::name('foster_order')->where('store_id',$this->dianpu_id)->count();//认养订单总数
        $count['check_with'] = Db::name('withdrawals')->where("status=-1 and type=2 and user_id=$this->user_id")->count();//待处理提现
        $count['with'] = Db::name('withdrawals')->where("status=1 and type=2 and user_id=$this->user_id")->count();//提现成功总数
        $count['with_sum'] = Db::name('withdrawals')->where("status=1 and type=2 and user_id=$this->user_id")->sum('money');//提现成功总额
        $count['goods'] =  Db::name('goods')->where("store_id=$this->dianpu_id and is_on_sale=1")->count();//商品总数
        $count['goods_stock'] =  Db::name('goods')->where("is_on_sale!=1 and store_id=$this->dianpu_id")->count();//商品总数
        $count['foster_goods'] =  Db::name('foster_goods')->where("store_id=$this->dianpu_id and is_on_sale=1")->count();//认养商品总数
        $count['foster_goods_stock'] =  Db::name('foster_goods')->where("is_on_sale!=1 and store_id=$this->dianpu_id")->count();//仓库中认养商品总数
        $count['user_money'] = Db::name('users')->where("user_id=$this->user_id")->value('user_money');//余额总额
        $count['jifen'] = Db::name('users')->where("user_id=$this->user_id")->value('jifen');//消费券总额
        $count['hy_yue'] = Db::name('users')->where("user_id=$this->user_id")->value('user_money');//商家余额总额
        $begin = date('Y-m-d', strtotime("-1 month"));//1个月前
        $end = date('Y-m-d', strtotime('+1 days'));
        $start_time=strtotime($begin);
        $end_time=strtotime($end);
        $orders = Db::name("order")
            ->field(" COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap ")
            ->where(" add_time >$start_time and add_time < $end_time  and order_status >0 and store_id=$this->dianpu_id")
            ->group('gap')
            ->select();
        foreach ($orders as $val1){
            $crr[$val1['gap']] = $val1['tnum'];
            $drr[$val1['gap']] = $val1['amount'];
        }
        for($i=$start_time;$i<=$end_time;$i=$i+24*3600){
            $tmp_num = empty($crr[date('Y-m-d',$i)]) ? 0 : $crr[date('Y-m-d',$i)];
            $tmp_amount = empty($drr[date('Y-m-d',$i)]) ? 0 : $drr[date('Y-m-d',$i)];
            $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
            $order_arr[] = $tmp_num;
            $amount_arr[] = $tmp_amount;
            $sign_arr[] = $tmp_sign;
            $date = date('Y-m-d',$i);
            $day[] = $date;
        }
        $order_result = ['order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day];
        View::assign('order_result',json_encode($order_result));
        View::assign('count', $count);
        return view();
    }
    public function verify(){
        return Captcha::create();
    }
    //获取下级分类
    public function get_category(){
        $parent_id = input('parent_id/d'); // 商品分类 父id
        $list = Db::name('goods_category')->where("parent_id", $parent_id)->select();
        if ($list) {
            throw new AdminException('获取成功',1,['result' => $list]);
        }
        throw new AdminException('获取失败');
    }
    //清空系统缓存
    public function cleanCache(){
        $runtimepath=runtime_path();
        delDir($runtimepath);
        Cache::clear();
        $this->success("清除成功");
    }
}
